package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.ReservaInstrumentoDao;
import edu.upc.clase.demo.entity.ReservaInstrumento;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;
/**
 *
 * @author NICOLAS
 */
@Repository
public class ReservaInstrumentoDaoImpl extends SimpleJdbcDaoSupport implements ReservaInstrumentoDao {
    private static Logger log = LoggerFactory.getLogger(ReservaInstrumentoDaoImpl.class);
    
    @Autowired
    public ReservaInstrumentoDaoImpl(DataSource dataSource) 
    {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }
     
    @Override
    public Integer insertar(ReservaInstrumento reservainstrumento) 
    {
        try {
        getJdbcTemplate().update(
        "insert into reserva_instrumento (coreserva, coinstrumento, qtinstrumento, flbasico) values (?,?,?,?)",
        reservainstrumento.getReserva().getCoreserva(),
        reservainstrumento.getInstrumento().getCoinstrumento(), 
        reservainstrumento.getQtinstrumento(), 
        reservainstrumento.getFlbasico());
        return 1;
        } catch (Exception e) {
            log.error("error al insertar");
        }
        return null;
    }    
    
    @Override
    public void actualizar(ReservaInstrumento reservainstrumento)
    {
        getJdbcTemplate().update(
                "update reserva_instrumento set qtinstrumento = ?, flbasico = ? where coreserva = ? and coinstrumento = ?",
                reservainstrumento.getQtinstrumento(), reservainstrumento.getFlbasico(), 
                reservainstrumento.getReserva().getCoreserva(), reservainstrumento.getInstrumento().getCoinstrumento());
    }
    
    @Override
    public void eliminar(ReservaInstrumento reservainstrumento)
    {
        getJdbcTemplate().update(
                "delete from reserva_instrumento where coreserva = ? and coinstrumento = ?", reservainstrumento.getReserva().getCoreserva(), reservainstrumento.getInstrumento().getCoinstrumento());
    }
    
    @Override
    public ReservaInstrumento buscar(Integer coreserva, Integer coinstrumento)
    {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select coreserva,coinstrumento,qtinstrumento, flbasico from reserva_instrumento where coreserva=? and coinstrumento=?",
                    new BeanPropertyRowMapper<ReservaInstrumento>(ReservaInstrumento.class), coreserva, coinstrumento);
        } catch (EmptyResultDataAccessException e) {
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}
